//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<XDesktop.cpp>
///	@path	~/src/gui/xgui/
///	@date	2008/09/07
///	@desc	.

#define _FULLAPI
#include "config/config.h"

#include "gui/xgui/XFrame.h"
#include "gui/xgui/XDesktop.h"

namespace xeres {
	
	IMPL_IOBJECT_CLASS( XDesktop );

	// XDesktop
	XDesktop::XDesktop( void )
	{
		m_updateRect.empty();
	}
	// ~XDesktop
	XDesktop::~XDesktop( void )
	{

	}
	// GetDesktopOrigin
	Pair<int> XDesktop::GetDesktopOrigin( void )
	{
		return Pair<int>( 0 , 0 );
	}
	// GetDesktopSize
	Pair<int> XDesktop::GetDesktopSize( void )
	{
		return GetSize();
	}
	// GetFocusArea
	Rect<int> XDesktop::GetFocusArea( void )
	{
		return GetPlacement();
	}
	// SetDesktopOrigin
	void XDesktop::SetDesktopOrigin( const Pair<int>& org )
	{

	}
	// SetDesktopSize
	void XDesktop::SetDesktopSize( const Pair<int>& size )
	{
		__super::SetSize( size );
	}
	// Attach
	void XDesktop::Attach( RefWeak<XFrame> frame )
	{

	}
	// Detach
	void XDesktop::Detach( RefWeak<XFrame> frame )
	{

	}
	// ShowWindow
	void XDesktop::ShowWindow( RefWeak<XFrame> frame , bool show , bool animate )
	{

	}
	// PinWindow
	void XDesktop::PinWindow( RefWeak<XFrame> frame , bool pin )
	{

	}
	// TrackTips
	void XDesktop::TrackTips( RefWeak<IGuiComp> comp , const WString& tips )
	{

	}
	// ClearTips
	void XDesktop::ClearTips( void )
	{

	}
	// OnKeyInput
	HandleInput XDesktop::OnKeyInput( Updater& updater )
	{
		const KeyInput& input = updater.GetKeyInput();
		if( m_onKey && m_onKey( input.GetState() , input.GetKey() , input.IsPressed() ) )
			return INPUT_HANDLED;

		return __super::OnKeyInput( updater );
	}
	// OnMouseInput
	HandleInput XDesktop::OnMouseInput( Updater& updater )
	{
		return __super::OnMouseInput( updater );
	}
	// OnMouseEnter
	void XDesktop::OnMouseEnter( Updater& updater , RefWeak<IGuiComp> last )
	{
		return __super::OnMouseEnter( updater , last );
	}
	// OnMouseLeave
	void XDesktop::OnMouseLeave( Updater& updater , RefWeak<IGuiComp> next )
	{
		return __super::OnMouseLeave( updater , next );
	}
	// OnFocusIn
	void XDesktop::OnFocusIn( Updater& updater , RefWeak<IGuiComp> last )
	{
		return __super::OnFocusIn( updater , last );
	}
	// OnFocusOut
	void XDesktop::OnFocusOut( Updater& updater , RefWeak<IGuiComp> next )
	{
		return __super::OnFocusOut( updater , next);
	}
	// SetPosition
	bool XDesktop::SetPosition( const Pair<int>& pos )
	{
		TRACE_WARNING( _S("XDesktop::SetPosition: Should call SetDesktopOrigin to set desktop position.") );
		return false;
	}
	// SetSize
	bool XDesktop::SetSize( const Pair<int>& size )
	{
		TRACE_WARNING( _S("XDesktop::SetSize: Should call SetDesktopSize to resize desktop.") );
		return false;
	}
	// SetPlacement
	bool XDesktop::SetPlacement( const Rect<int>& placement )
	{
		TRACE_WARNING( _S("XDesktop::SetPlacement: Should call SetDesktopSize & SetDesktopOrigin to set desktop placement.") );
		return false;
	}
	// SetParent
	bool XDesktop::SetParent( RefWeak<IGuiForm> parent )
	{
		TRACE_ERROR( _S("XDesktop::SetParent: There's no parent of desktop.") );
		return false;
	}

	// RequestShow
	void XDesktop::RequestShow( RefWeak<IGuiComp> comp , bool show )
	{

	}

	// RequestUpdate
	void XDesktop::RequestUpdate( RefWeak<IGuiComp> comp , const Rect<int>& region )
	{
		XS_ASSERT( comp->GetParent() == this );

		Rect<int> r( region );
		r.offset( comp->GetPosition() );
		r = r & GetPlacement();

		if( r.is_empty() )
			return;

		if( m_updateRect.is_empty() )
			m_updateRect = r;
		else
			m_updateRect = m_updateRect | r;
	}

	// ValidateDraw
	void XDesktop::ValidateDraw( void )
	{
		m_updateRect.empty();
	}

	// AddUpdateRegion
	void XDesktop::AddUpdateRegion( const Rect<int>& region )
	{
		if( m_updateRect.is_empty() )
			m_updateRect = region;
		else
			m_updateRect = m_updateRect | region;
	}

	// GetUpdateRegion
	const Rect<int>& XDesktop::GetUpdateRegion( void )
	{
		return m_updateRect;
	}
	// AddChild
	bool XDesktop::AddChild( RefWeak<IGuiComp> comp )
	{
		TRACE_ERROR( _S("XDesktop::AddChild: Should call Attach to attach window on desktop.") );
		return false;
	}
	// DelChild
	bool XDesktop::DelChild( RefWeak<IGuiComp> comp )
	{
		TRACE_ERROR( _S("XDesktop::DelChild: Should call Detach to attach window on desktop.") );
		return false;
	}

	// OnClose
	void XDesktop::OnClose( void )
	{
		m_onKey = NULL;
		__super::OnClose();
	}
	// OnChildRepose
	void XDesktop::OnChildRepose( RefWeak<XFrame> comp )
	{

	}

} // namespace xeres